﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using StaticPlugin;
using System.Windows.Forms;

namespace MosWar
{
    class jPoliceConnections : AJob
    {
        string status = "idle";
        jPoliceConnectionsSetup setup = new jPoliceConnectionsSetup();

        #region Interface with Executer

        protected override string getName()
        {
            return "Взятки ментам";
        }

        protected override string getStatus()
        {
            return status;
        }

        protected override bool getAvailable()
        {
            return (bool)Properties.Settings.Default["cbBribedAvailable"];
        }

        protected override int getPriority()
        {
            return int.Parse(Properties.Settings.Default["numBridePriority"].ToString());
        }

        public override bool testRequirements(WebBrowser b)
        {
            int moneyAfterBribe = int.Parse(Properties.Settings.Default["numBalansAfterBribe"].ToString());
            if (b.Document == null)
            {
                return false;
            }
            if (b.Document.Body == null)
            {
                return false;
            }
            if (b.IsBusy)
            {
                return false;
            }
            HtmlElement Person = b.Document.Body.GetElementsByTagName("div")["personal"];
            HtmlElement wanted = WebParserEx.getTagByClassName(Person, "div", "wanted");
            HtmlElement persent = WebParserEx.getTagByClassName(wanted, "div", "percent");
            if (persent == null) 
            {
                status = "Не смогли определить степень розыска";
                return false;
            }

            int wantedPersent = -1;
            try
            {
                wantedPersent = int.Parse(persent.Style.Split(' ')[1].Split('%')[0]);
            }
            catch (Exception)
            {
                status = "Не смогли распознать степень розыска";
                return false;
            }
            status = "Вероятность попасть за решетку: " + wantedPersent.ToString() + "%";
            if (wantedPersent > 39 && SkillsDetector.getMoney(b) > 300 + moneyAfterBribe) return true;
            return false;
        }

        public override DialogResult showSetupDialog()
        {
            return setup.ShowDialog();
        }

        public override void initActions()
        {
            actions.Clear();
            actions.Add(new Action("Идем в полицию", GoToPolice));
            actions.Add(new Action("Даем взятку", GiveABribe));
        } 

        #endregion

        void GoToPolice(WebBrowser b) 
        {
            if (b.Url.AbsolutePath != "/police/") b.Navigate("www.moswar.ru/police/");
            status = "Идем в полицию";
        }

        void GiveABribe(WebBrowser b) 
        {
            HtmlElement PoliceAttention = WebParserEx.getTagByClassName(b.Document.Body, "form", "police-attention");
            if (PoliceAttention == null) 
            {
                Console.WriteLine("internal error: cannot find police-attention!");
                status = "ERROR: cannot find police-attention";
                return;
            }
            HtmlElement btnGiveABride = WebParserEx.getTagByClassName(PoliceAttention, "div", "button");
            if (btnGiveABride == null)
            {
                Console.WriteLine("internal error: cannot find bribe button!");
                status = "ERROR: cannot find bribe button";
                return;
            }
            btnGiveABride.InvokeMember("click");
            int z=0;
            try
            {
                z = int.Parse(Properties.Settings.Default["edTotalBribes"].ToString());
            }
            catch (Exception)
            {
                z = 0;
            }
            Properties.Settings.Default["edTotalBribes"] = (z + 1).ToString();
            Properties.Settings.Default.Save();
        }

    }
}
